package com.qf.smsplatform.service.Impl;
/*2021/10/29*/

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.smsplatform.cache.TemplateCache;
import com.qf.smsplatform.constans.ResultCode;
import com.qf.smsplatform.events.TSmsTemplateEvent;
import com.qf.smsplatform.exception.AddDataException;
import com.qf.smsplatform.exception.QueryDataException;
import com.qf.smsplatform.exception.UpdateDataException;
import com.qf.smsplatform.mapper.TSmsTemplateMapper;
import com.qf.smsplatform.pojo.CheckType;
import com.qf.smsplatform.pojo.MyAuthUser;
import com.qf.smsplatform.pojo.TSmsTemplate;
import com.qf.smsplatform.service.TSmsTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @YuTao
 */
@Service
@Transactional
public class TSmsTemplateServiceImpl implements TSmsTemplateService {

    private TSmsTemplateMapper smsTemplateMapper;
    @Autowired
    public void setSmsTemplateMapper(TSmsTemplateMapper smsTemplateMapper) {
        this.smsTemplateMapper = smsTemplateMapper;
    }

    private TemplateCache templateCache;
    @Autowired
    public void setTemplateCache(TemplateCache templateCache) {
        this.templateCache = templateCache;
    }

    private ApplicationContext context;
    @Autowired
    public void setContext(ApplicationContext context) {
        this.context = context;
    }
    @Override
    public void insetTSmsTemplate(TSmsTemplate tSmsTemplate) {
        if(tSmsTemplate.isNull(CheckType.ADD)){
            throw new AddDataException("添加的数据为空", ResultCode.DATA_ISNULL);
        }
        smsTemplateMapper.insertTemplate(tSmsTemplate);
        context.publishEvent(new TSmsTemplateEvent());
    }

    @Override
    public void updateTSmsTemplate(TSmsTemplate tSmsTemplate) {
        TSmsTemplate byKey = templateCache.getByKey(tSmsTemplate.getId());//查出当前修改的模板信息，然后判断是否有权限进行修改
        if(ObjectUtils.isEmpty(byKey)){
            throw new QueryDataException("数据库中不存在需要修改的模板信息", ResultCode.DATA_ISNULL);
        }
        if (!tSmsTemplate.isNull(CheckType.UPDATE)&&tSmsTemplate.getCreater()!=byKey.getCreater()){
            throw new UpdateDataException("没有权限进行修改", ResultCode.NOT_ALLOW);
        }
        smsTemplateMapper.updataTemplate(tSmsTemplate);

        context.publishEvent(new TSmsTemplateEvent());
    }
    @Override
    public void deleteTemplate(Long id){
        TSmsTemplate byKey = templateCache.getByKey(id);//查出当前修改的模板信息，然后判断是否有权限进行修改
        if(ObjectUtils.isEmpty(byKey)){
            throw new QueryDataException("没有查询到当前序号的模板信息", ResultCode.DATA_ISNULL);
        }
        //拿到当前登录用户的信息，与需要删除的模板信息创造者进行对比，判断是否具有删除的权限
        MyAuthUser user = (MyAuthUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        //判断是否有权限进行删除
        if (user.getType()==2&&user.getId()!=byKey.getCreater()){
            throw new UpdateDataException("没有权限进行模板信息删除", ResultCode.NOT_ALLOW);
        }
        //不抛异常，说明有权限尽心删除
        smsTemplateMapper.deleteTemplate(id);
        //发布更新缓存的事件
        context.publishEvent(new TSmsTemplateEvent());
    }

    @Override
    public PageInfo<TSmsTemplate> selectPage(int pageNum, int pageSize) {
        //获取当前登录用户的信息，判断那些数据能够看到，那些数据无法看到
        MyAuthUser user = (MyAuthUser)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        //限制查询的数据量
        PageHelper.startPage(pageNum, pageSize);
        List<TSmsTemplate> allData = templateCache.getAllData();
        //对查询出来的信息进行具体的判断
        List<TSmsTemplate> collect = allData.stream().filter(tSmsTemplate -> {
            return user.getType()==2 || user.getId() == tSmsTemplate.getCreater();
        })
                .skip((pageNum - 1) * pageSize)
                .limit(pageSize)
                .collect(Collectors.toList());

        PageInfo<TSmsTemplate> pageInfo=new PageInfo<>(collect);

        return pageInfo;

    }

}
